package com.shangcheng.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.mappers.SmsHomeRecommendProductMapper;
import com.shangcheng.model.domain.SmsHomeNewProduct;
import com.shangcheng.model.domain.SmsHomeRecommendProduct;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/13 11:53
 */
@DubboService
public class SmsHomeRecommendProductApiImpl implements SmsHomeRecommendProductApi {
    @Autowired
    private SmsHomeRecommendProductMapper smsHomeRecommendProductMapper;
    /**
     * 批量添加首页推荐
     * @param homeRecommendProductList
     */
    @Override
    public void createRecPro(List<SmsHomeRecommendProduct> homeRecommendProductList) {
        for (SmsHomeRecommendProduct product : homeRecommendProductList) {
            smsHomeRecommendProductMapper.insert(product);
        }
    }

    /**
     * 批量删除首页推荐
     * @param ids
     */
    @Override
    public void deleteRecPro(Integer[] ids) {
        smsHomeRecommendProductMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 分页查询推荐
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    @Override
    public IPage<SmsHomeRecommendProduct> RecProList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        IPage<SmsHomeRecommendProduct> page= new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeRecommendProduct> wrapper= new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(productName),SmsHomeRecommendProduct::getProductName,productName);
        wrapper.eq(!ObjectUtils.isEmpty(recommendStatus),SmsHomeRecommendProduct::getRecommendStatus,recommendStatus);
        return smsHomeRecommendProductMapper.selectPage(page,wrapper);
    }

    /**
     * 批量修改推荐状态
     * @param ids
     * @param recommendStatus
     */
    @Override
    public void updateRecRecommendStatus(Integer[] ids, Integer recommendStatus) {
        for (Integer id : ids) {
            SmsHomeRecommendProduct product =new SmsHomeRecommendProduct();
            product.setId(id.longValue());
            product.setRecommendStatus(recommendStatus);
            smsHomeRecommendProductMapper.updateById(product);
        }
    }

    /**
     * 修改推荐排序
     * @param id
     * @param sort
     */
    @Override
    public void updateRecSort(Integer id, Integer sort) {
        SmsHomeRecommendProduct product=new SmsHomeRecommendProduct();
        product.setId(id.longValue());
        product.setSort(sort);
        LambdaUpdateWrapper<SmsHomeRecommendProduct> wrapper= new LambdaUpdateWrapper<>();
        wrapper.eq(SmsHomeRecommendProduct::getId,product.getId());
        wrapper.set(!ObjectUtils.isEmpty(sort),SmsHomeRecommendProduct::getSort,product.getSort());
        smsHomeRecommendProductMapper.update(product,wrapper);
    }

    /**
     * 根据产品名称查询推荐产品
     * @param homeRecommendProductList
     * @return
     */
    @Override
    public List<SmsHomeRecommendProduct> findByName(List<SmsHomeRecommendProduct> homeRecommendProductList) {
        List<String> productNames = CollUtil.getFieldValues(homeRecommendProductList, "productName", String.class);
        LambdaQueryWrapper<SmsHomeRecommendProduct> wrapper= new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeRecommendProduct::getProductName,productNames);
        return smsHomeRecommendProductMapper.selectList(wrapper);
    }
}
